package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlQuestion entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlQuestion
 * @author MyEclipse Persistence Tools
 */

public class MdlQuestionDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlQuestionDAO.class);
	// property constants
	public static final String CATEGORY = "category";
	public static final String PARENT = "parent";
	public static final String NAME = "name";
	public static final String QUESTIONTEXT = "questiontext";
	public static final String QUESTIONTEXTFORMAT = "questiontextformat";
	public static final String GENERALFEEDBACK = "generalfeedback";
	public static final String GENERALFEEDBACKFORMAT = "generalfeedbackformat";
	public static final String DEFAULTMARK = "defaultmark";
	public static final String PENALTY = "penalty";
	public static final String QTYPE = "qtype";
	public static final String LENGTH = "length";
	public static final String STAMP = "stamp";
	public static final String VERSION = "version";
	public static final String HIDDEN = "hidden";
	public static final String TIMECREATED = "timecreated";
	public static final String TIMEMODIFIED = "timemodified";
	public static final String CREATEDBY = "createdby";
	public static final String MODIFIEDBY = "modifiedby";

	public void save(MdlQuestion transientInstance) {
		log.debug("saving MdlQuestion instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(MdlQuestion persistentInstance) {
		log.debug("deleting MdlQuestion instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlQuestion findById(java.lang.Long id) {
		log.debug("getting MdlQuestion instance with id: " + id);
		try {
			MdlQuestion instance = (MdlQuestion) getSession().get(
					"idv.loveloser.dao.MdlQuestion", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlQuestion instance) {
		log.debug("finding MdlQuestion instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlQuestion")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlQuestion instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from MdlQuestion as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCategory(Object category) {
		return findByProperty(CATEGORY, category);
	}

	public List findByParent(Object parent) {
		return findByProperty(PARENT, parent);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByQuestiontext(Object questiontext) {
		return findByProperty(QUESTIONTEXT, questiontext);
	}

	public List findByQuestiontextformat(Object questiontextformat) {
		return findByProperty(QUESTIONTEXTFORMAT, questiontextformat);
	}

	public List findByGeneralfeedback(Object generalfeedback) {
		return findByProperty(GENERALFEEDBACK, generalfeedback);
	}

	public List findByGeneralfeedbackformat(Object generalfeedbackformat) {
		return findByProperty(GENERALFEEDBACKFORMAT, generalfeedbackformat);
	}

	public List findByDefaultmark(Object defaultmark) {
		return findByProperty(DEFAULTMARK, defaultmark);
	}

	public List findByPenalty(Object penalty) {
		return findByProperty(PENALTY, penalty);
	}

	public List findByQtype(Object qtype) {
		return findByProperty(QTYPE, qtype);
	}

	public List findByLength(Object length) {
		return findByProperty(LENGTH, length);
	}

	public List findByStamp(Object stamp) {
		return findByProperty(STAMP, stamp);
	}

	public List findByVersion(Object version) {
		return findByProperty(VERSION, version);
	}

	public List findByHidden(Object hidden) {
		return findByProperty(HIDDEN, hidden);
	}

	public List findByTimecreated(Object timecreated) {
		return findByProperty(TIMECREATED, timecreated);
	}

	public List findByTimemodified(Object timemodified) {
		return findByProperty(TIMEMODIFIED, timemodified);
	}

	public List findByCreatedby(Object createdby) {
		return findByProperty(CREATEDBY, createdby);
	}

	public List findByModifiedby(Object modifiedby) {
		return findByProperty(MODIFIEDBY, modifiedby);
	}

	public List findAll() {
		log.debug("finding all MdlQuestion instances");
		try {
			String queryString = "from MdlQuestion";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlQuestion merge(MdlQuestion detachedInstance) {
		log.debug("merging MdlQuestion instance");
		try {
			MdlQuestion result = (MdlQuestion) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MdlQuestion instance) {
		log.debug("attaching dirty MdlQuestion instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlQuestion instance) {
		log.debug("attaching clean MdlQuestion instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}